import {fileName} from "./suffix";
import fetchBlob from 'rn-fetch-blob';
import {RNFFmpeg} from 'react-native-ffmpeg';
import CameraRoll from "@react-native-community/cameraroll";

RNFFmpeg.disableLogs();

const {
    fs
} = fetchBlob;

interface Stream {
    width: number,
    height: number,
}

interface Info extends Stream {
    fps: number,
    size: number,
    duration: number
}

//视频封面图片文件夹
export const COVER_DIR = `${fs.dirs.CacheDir}/VIDEO_COVER_DIR`;

//视频压缩文件夹
export const COMPRESS_DIR = `${fs.dirs.CacheDir}/VIDEO_COMPRESS_DIR`;

//水印图片文件夹
export const WATER_MARK_DIR = `${fs.dirs.CacheDir}/WATER_MARK_DIR`;

//创建文件夹
export const createFolder = async (folder: string) => {
    const exists = await fs.exists(folder);
    if (!exists) await fs.mkdir(folder);
    return folder;
}

//获取详情
export const info = async (path: string): Promise<Info> => {
    try {
        const {
            bitrate,
            duration,
            streams = []
        } = await (
            RNFFmpeg.getMediaInformation(path)
        ) as any
        const {
            width = 0,
            height = 0
        } = streams[0] as Stream;

        const seconds = duration / 1000;

        const _size = (
            bitrate * seconds / 8 / 1000
        )
        const _res = {
            size: _size,
            width: width,
            fps: bitrate,
            height: height,
            duration: Math.ceil(seconds)
        }
        return _res;
    }
    catch (e) {
        return Promise.reject(e);
    }
}

//复制水印图片
const copyWaterMark = async () => {
    const _waterMarkPath = fs.asset(`water@2x.png`);
    const _distPath = await createFolder(WATER_MARK_DIR);
    const _output = `${_distPath}/logo2x.png`;
    if (!await fs.exists(_output)) {
        await  fs.cp(_waterMarkPath, _output);
    }
    return _output;
}

//获取视频的封面
export const cover = async (path: string) => {
    const _folder = await createFolder(COVER_DIR);
    const _name = fileName(path).replace(/.mp4/,'.jpg');
    const _output = `${_folder}/${_name}`;
    const _cmd = `-y -i ${path} -ss 00:00:01 -vframes 1 ${_output}`;
    const {
        rc
    } = await RNFFmpeg.execute(_cmd);
    if (rc !== 0) return ``;
    return `file://${_output}`
}

//压缩视频和加水印
export const compress = async (path: string) => {
    const {
        fps,
        height
    } = await info(path);
    const _logo = await copyWaterMark();
    const _folder = await createFolder(COMPRESS_DIR);
    const _output = `${_folder}/${fileName(path)}`;

    const _sizeCmd = height > 720 ? `-s 1280x720` : ``;

    const _markCmd = `-i ${_logo} -filter_complex 'overlay=x=main_w-overlay_w-20:y=main_h-overlay_h-20'`;

    const _cmd = `-y -threads 0 -i ${path} ${_sizeCmd} ${_markCmd} -b ${fps / 2.5}k ${_output}`;

    const {
        rc
    } = await RNFFmpeg.execute(_cmd);

    if (rc !== 0) return path;

    return `file://${_output}`
}

//删除文件夹
export const deleteFile = async (path: string) => {
    const exists = await fs.exists(path);
    if(exists) {
        fs.unlink(path)
    }
}

//保存视频到相册
export const saveVideoToAlbum = (path:string) => {
   return CameraRoll.saveToCameraRoll(path,'video');
}
